﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class ManageBooks : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string libID = this.Request.QueryString.Get("id");
        lblLibID.Text = libID;
        lblConfirm.Text = "";
        CrystalReportSource1.DataBind();
        CrystalReportViewer1.DataBind();
        
    }

    protected void btnNewPublisher_Click(object sender, EventArgs e)
    {

    }

    protected void DetailsView1_onItemCommand(object sender, DetailsViewCommandEventArgs e)
    {
        if (e.CommandName == "Cancel")
        {
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
            DetailsView1.DataBind();
        }
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        txtFindBook.Text = "";
        lblNoResults.Text = "";
        DetailsView1.ChangeMode(DetailsViewMode.Insert);
    }

    protected void btnFindBook_Click(object sender, EventArgs e)
    {

        ManageBook manager = new ManageBook();
        
        try
        {
            int.Parse(txtFindBook.Text);
        }
        catch (Exception ex)
        {
            if (ex.Message == "Input string was not in a correct format.")
            {
                SqlDataAdapter dat = manager.LibrarianSearchBook(txtFindBook.Text);
                DataSet d = new DataSet();
                dat.Fill(d);

                GridView1.DataSource = d;
                GridView1.DataBind();
            }
            else
            {
                SqlDataAdapter datISBN = manager.SearchByISBN(txtFindBook.Text);
                DataSet dISBN = new DataSet();
                datISBN.Fill(dISBN);

                GridView1.DataSource = dISBN;
                GridView1.DataBind();
            }
        }

        

        if (GridView1.Rows.Count <= 0)
        {
            lblNoResults.Text = "There are no results that match your search.";
        }
        else
        {
            lblNoResults.Text = "";
        }
    }

    protected void GridView1_SelectedIndexChanged(object sender, GridViewCommandEventArgs e)
    {
        BindDetailsView(e);
    }

    protected void BindDetailsView(GridViewCommandEventArgs e)
    {
        int rowIndex = Convert.ToInt32(e.CommandArgument.ToString());
        ViewState["Barcode"] = GridView1.DataKeys[rowIndex].Value.ToString();
        ManageBook manager = new ManageBook();
        SqlDataAdapter dat = manager.SearchByBarcode(ViewState["Barcode"].ToString());
        DataSet d = new DataSet();
        dat.Fill(d);

        DetailsView1.DataSource = d;
        DetailsView1.DataBind();
    }

    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        DataKey dk = DetailsView1.DataKey;
        DetailsView1.ChangeMode(e.NewMode);
        //DetailsView1.DataBind();

        if (e.NewMode == DetailsViewMode.Edit)
        {
            ManageBook manager = new ManageBook();
            SqlDataAdapter dat = manager.SearchByBarcode(dk.ToString());
            DataSet d = new DataSet();
            dat.Fill(d);

            DetailsView1.DataSource = d;
            DetailsView1.DataBind();
        }
        
    }

    //catches error when details view is updated and displays save confirmation message
    protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        Book uBook = new Book();
        uBook.LibraryID = Convert.ToInt32(Request.QueryString["id"]);

        uBook.Barcode = DetailsView1.Rows[0].Cells[1].Controls[0].ToString();
        uBook.ISBN = Convert.ToInt32(DetailsView1.Rows[1].Cells[1].Controls[0].ToString());
        uBook.Title = DetailsView1.Rows[2].Cells[1].Controls[0].ToString();
        uBook.Author = DetailsView1.Rows[3].Cells[1].Controls[0].ToString();
        uBook.SeriesTitle = DetailsView1.Rows[3].Cells[1].Controls[0].ToString();
        uBook.CallNumber = DetailsView1.Rows[3].Cells[1].Controls[0].ToString();
        uBook.DatePublished = Convert.ToDateTime(DetailsView1.Rows[3].Cells[1].Controls[0].ToString());
        uBook.Subject = DetailsView1.Rows[3].Cells[1].Controls[0].ToString();
        uBook.Keywords = DetailsView1.Rows[3].Cells[1].Controls[0].ToString();
        uBook.isYoungAdult = Convert.ToBoolean(DetailsView1.Rows[3].Cells[1].Controls[0].ToString());

        ManageBook mBook = new ManageBook();
        mBook.updateBook(uBook);


        //if (e..Exception != null)
        //{
        //    lblConfirm.Text = "A database error has occured. <br /><br /> Message: " + e.Exception.Message;
        //    e.ExceptionHandled = true;
        //    e.KeepInEditMode = true;
        //}
        //else if (e.AffectedRows == 0)
        //    lblConfirm.Text = "Another user may have updated that record. <br /> Please try again.";
        //else
            lblConfirm.Text = "Your record has been saved";
        GridView1.DataBind();
    }

    //catches error when a new record is inserted in details view and displays save confirmation message
    protected void DetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        Book uBook = new Book();
        uBook.LibraryID = Convert.ToInt32(Request.QueryString["id"]);
        //int i = Convert.ToInt32(DetailsView1.Rows[1].Cells[1].Controls[0].ToString());
        //long l = i;

        uBook.Barcode = ((TextBox)DetailsView1.Rows[0].Cells[1].Controls[0]).Text;
        uBook.ISBN = Convert.ToInt32(((TextBox)DetailsView1.Rows[1].Cells[1].Controls[0]).Text);
        uBook.Title = ((TextBox)DetailsView1.Rows[2].Cells[1].Controls[0]).Text;
        uBook.Author = ((TextBox)DetailsView1.Rows[3].Cells[1].Controls[0]).Text;
        uBook.SeriesTitle = ((TextBox)DetailsView1.Rows[4].Cells[1].Controls[0]).Text;
        uBook.CallNumber = ((TextBox)DetailsView1.Rows[5].Cells[1].Controls[0]).Text;
        uBook.DatePublished = Convert.ToDateTime(((TextBox)DetailsView1.Rows[6].Cells[1].Controls[0]).Text);
        uBook.Subject = ((TextBox)DetailsView1.Rows[7].Cells[1].Controls[0]).Text;
        uBook.Keywords = ((TextBox)DetailsView1.Rows[8].Cells[1].Controls[0]).Text;
        uBook.isYoungAdult = Convert.ToBoolean(((CheckBox)DetailsView1.Rows[9].Cells[1].Controls[0]).Checked);

        ManageBook mBook = new ManageBook();
        mBook.insertBook(uBook);

        //if (e.Exception != null)
        //{
        //    //if (e.Exception.Message.ToString() == "Violation of PRIMARY KEY constraint 'PK_Person'. Cannot insert duplicate key in object 'dbo.PERSON'. Violation of PRIMARY KEY constraint 'PK_Employee'. Cannot insert duplicate key in object 'dbo.EMPLOYEE'. The statement has been terminated. The statement has been terminated.")
        //    //{
        //    lblConfirm.Text = "This Id number has already been used.";
        //    e.ExceptionHandled = true;
        //    e.KeepInInsertMode = true;
        //    //}
        //}
        //else if (e.AffectedRows == 0)
        //    lblConfirm.Text = "Another user may have updated that record. <br /> Please try again.";
        //else
            lblConfirm.Text = "Your record has been saved";
        GridView1.DataBind();
        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
        DetailsView1.DataBind();
    }

    protected void btnRpt_Click(object sender, EventArgs e)
    {
        pnlReport.Visible = true;
    }
}
